// pages/ser-detail-add/ser-detail-add.js
var app = getApp();
var main = require("../../../main.js");
var WxParse = require('../../../wxParse/wxParse.js');
const config = app.globalData;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    actions:[],
    show: false,
    show1: false,
    show2: false,
    type:'',
    c_horizontal:'',
    c_name: '',
    c_tname: '',
    c_img: '',
    c_vertical: '',
    selectTopName:'局部阻力系数查询',
    selectTopNamet:'局部阻力系数查询',
    selectTopName1:'',
    selectTopName2:'',
    fangyuan:'请选择',
    title:'局部阻力计算',
    id:'',
    del_show_flg:0,
    s_name1: '',
    s_name2: '',
    s_number: '',
    num1: '',
    num2: 0,
    num3: '',
    num4: '1.205',
    num5: '',
    num6: '',
    num7: '',
    num8:'',
    r_num1: '',
    r_num2: '',
    r_num3: '',
    r_num4: '',
    c_type:0,
    news:'',
    actions1:[
      {
        id: 1,
        name: '方管'
      },
      {
        id: 2,
        name: '圆管'
      }
    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.checkUserAuthorization();
    this.getTypeImg();
    this.getnews();
    this.setData({
      id: options.id,
      del_show_flg: options.del_show_flg,
      s_number: options.s_number,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  // 浏览大图
  onClick: function(e) {
    console.log(e, 'e')
    wx.previewImage({
      urls: [this.data.c_img]
    })
  },
  // 弹出
  onChange: function(e) {
    const type = e.currentTarget.dataset.type;
    const that = this;
    switch(type) {
      case 1: 
        if(that.data.c_name === ''){
          wx.showToast({
            title: "请先选择局部阻力系数查询",
            icon: 'none',
            duration: 3000
          })
          return false;
        }
  
        wx.request({
          url: app.domain_path + app.api_path + '/get_drag_coefficient_c_horizontal',
          method: 'post',
          data: {
            token: main.get_storage('token'),
            c_name: that.data.c_name,
            c_vertical:that.data.c_vertical,
            c_type:that.data.c_type
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success: function(res) {
            console.log(res);
            if (!res.data) {
              wx.showToast({
                title: '加载错误',
                icon: 'loading',
                duration: 3000
              })
            }
            if (res.data.RESULT == 'OK') {
              wx.hideLoading();
              that.setData({
                actions: res.data.data.drag_coefficient_arr1,
                show: true,
                type
              })
            } else {
              wx.showToast({
                title: res.data.MESSAGE,
                icon: 'none',
                duration: 3000
              })
            }
          }
        })
        break;
      case 2:
        if(that.data.c_name === ''){
          wx.showToast({
            title: "请先选择局部阻力系数查询",
            icon: 'none',
            duration: 3000
          })
          return false;
        }
        wx.request({
          url: app.domain_path + app.api_path + '/get_drag_coefficient_c_vertical',
          method: 'post',
          data: {
            token: main.get_storage('token'),
            c_name: that.data.c_name,
            c_horizontal: that.data.c_horizontal
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success: function(res) {
            console.log(that.data.c_horizontal);
            if (!res.data) {
              wx.showToast({
                title: '加载错误',
                icon: 'loading',
                duration: 3000
              })
            }
            if (res.data.RESULT == 'OK') {
              wx.hideLoading();
              that.setData({
                actions: res.data.data.drag_coefficient_arr1,
                show: true,
                type
              })
            } else {
              wx.showToast({
                title: res.data.MESSAGE,
                icon: 'none',
                duration: 3000
              })
            }
          }
        })
        break;
      case 3:
        wx.request({
          url: app.domain_path + app.api_path + '/get_drag_coefficient1',
          method: 'post',
          data: {
            token: main.get_storage('token'),
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success: function(res) {
            console.log(res);
            if (!res.data) {
              wx.showToast({
                title: '加载错误',
                icon: 'loading',
                duration: 3000
              })
            }
            if (res.data.RESULT == 'OK') {
              wx.hideLoading();
              that.setData({
                actions: res.data.data.drag_coefficient_arr1,
                show: true,
                type
              })
            } else {
              wx.showToast({
                title: res.data.MESSAGE,
                icon: 'none',
                duration: 3000
              })
            }
          }
        })
        break;
      case 4:
        wx.request({
          url: app.domain_path + app.api_path + '/get_drag_coefficient2',
          method: 'post',
          data: {
            token: main.get_storage('token'),
            c_tname: that.data.c_tname
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success: function(res) {
            console.log(res);
            if (!res.data) {
              wx.showToast({
                title: '加载错误',
                icon: 'loading',
                duration: 3000
              })
            }
            if (res.data.RESULT == 'OK') {
              wx.hideLoading();
              that.setData({
                actions: res.data.data.drag_coefficient_arr1,
                show: true,
                type
              })
            } else {
              wx.showToast({
                title: res.data.MESSAGE,
                icon: 'none',
                duration: 3000
              })
            }
          }
        })
        break;
        case 5:
          that.setData({
            actions: that.data.actions1,
            show: true,
            type
          })
        break;
    }
  },
  onClose() {
    this.setData({ show: false });
  },

  onSelect(event) {
    const { type } = this.data;
    switch(type) {
      case 1: 
      console.log(event);
        const c_horizontal = event.detail.name;
        const sc_horizontal = event.detail.sname;
        this.setData({
          selectTopName1: c_horizontal,
          c_horizontal: sc_horizontal,
          num2: event.detail.c_values
        })
        this.onClose();
        break;
      case 2:
        const c_vertical = event.detail.name;
        const sc_vertical = event.detail.sname;
        this.setData({
          selectTopName2: c_vertical,
          c_vertical: sc_vertical,
          num2: event.detail.c_values
        })
        this.onClose();
        break;
      case 3:
        const c_tname = event.detail.name;
        const c_name1 = event.detail.c_name;
        this.setData({
          selectTopName: c_tname,
          selectTopNamet: c_name1,
          selectTopName1: event.detail.c_1,
          selectTopName2: event.detail.c_2,
          c_tname: c_tname,
          c_name: c_name1,
          c_img: event.detail.c_img,
          c_type:event.detail.c_type,
          num2: event.detail.c_values
        })
        this.onClose();
        break;
      case 4:
        const c_name = event.detail.name;
        this.setData({
          selectTopNamet: c_name,
          selectTopName1: event.detail.c_1,
          selectTopName2: event.detail.c_2,
          c_name: c_name,
          c_img: event.detail.c_img,
          c_type:event.detail.c_type,
          num2: event.detail.c_values
        })
        this.onClose();
        break;
      case 5:
          if(event.detail.name=='圆管'){
            this.setData({
              fangyuan:event.detail.name,
              show1:true,
              show2:false
            })           
          }else{
            this.setData({
              fangyuan:event.detail.name,
              show1:false,
              show2:true
            })            
          }
          this.onClose();
          break;
    }
  },

  goBack: function() {
    wx.switchTab({
      url: '/pages/ser-list/ser-list'
    });
  },

  goAdd:function(){
    var that = this;
    wx.showLoading({
      title: '加载中',
    })
    if(that.data.r_num1 === '' && that.data.r_num2 === ''){
      wx.hideLoading();
      wx.showToast({
        title: '当前页面没有计算结果，无法添加',
        icon: 'none',
        duration: 3000
      })
    }else{
      wx.request({
        url: app.domain_path + app.api_path + '/smoke_ventilation_insert',
        method: 'post',
        data: {
          token: main.get_storage('token'),
          smoke_type:41,
          s_name1: that.data.s_name1,
          s_name2: that.data.s_name2,
          s_number: that.data.s_number,
          c_horizontal:that.data.c_horizontal,
          c_name: that.data.c_name,
          c_tname: that.data.c_tname,
          c_vertical: that.data.c_vertical,
          c_img:that.data.c_img,
          num1: that.data.num1,
          num2: that.data.num2,
          num3: that.data.num3,
          num4: that.data.num4,
          num5: that.data.num5,
          num6: that.data.num6,
          num7: that.data.num7,
          num8: that.data.num8,
          num9: that.data.fangyuan,
          r_num1: that.data.r_num1,
          r_num2: that.data.r_num2,
          r_num3: that.data.r_num3,
          r_num4: that.data.r_num4
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function(res) {
          if (!res.data) {
            wx.showToast({
              title: '加载错误',
              icon: 'loading',
              duration: 3000
            })
          }
          if (res.data.RESULT == 'OK') {
            wx.hideLoading();
            console.log(res.data.data);
            wx.navigateTo({
              url: "/pages/xitong/ser-detail-add/ser-detail-add?del_show_flg=1&id="+ that.data.id + "&s_number=" + res.data.data
            });
          } else {
            wx.showToast({
              title: res.data.MESSAGE,
              icon: 'none',
              duration: 3000
            })
          }
        }
      })
    }
  },

  goDelete:function(){
    wx.navigateBack({
      delta: 1,
      success: function() {
        wx.navigateTo({
          url: '/pages/currentPage/currentPage',
        });
      }
    });
  },

  goSave:function(){
    var that = this;
    wx.showLoading({
      title: '加载中',
    })
    
    if(that.data.r_num1 === '' && that.data.r_num2 === ''){
      wx.hideLoading();
      wx.showToast({
        title: '没有计算结果，无法保存。',
        icon: 'none',
        duration: 3000
      })
    }else{
      wx.request({
        url: app.domain_path + app.api_path + '/smoke_ventilation_insert',
        method: 'post',
        data: {
          token: main.get_storage('token'),
          smoke_type:41,
          s_name1: that.data.s_name1,
          s_name2: that.data.s_name2,
          s_number: that.data.s_number,
          c_horizontal:that.data.c_horizontal,
          c_name: that.data.c_name,
          c_tname: that.data.c_tname,
          c_vertical: that.data.c_vertical,
          c_img:that.data.c_img,
          num1: that.data.num1,
          num2: that.data.num2,
          num3: that.data.num3,
          num4: that.data.num4,
          num5: that.data.num5,
          num6: that.data.num6,
          num7: that.data.num7,
          num8: that.data.num8,
          num9: that.data.fangyuan,
          r_num1: that.data.r_num1,
          r_num2: that.data.r_num2,
          r_num3: that.data.r_num3,
          r_num4: that.data.r_num4
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function(res) {
          if (!res.data) {
            wx.showToast({
              title: '加载错误',
              icon: 'loading',
              duration: 3000
            })
          }
          if (res.data.RESULT == 'OK') {
            wx.hideLoading();
            wx.showToast({
              title: '保存成功',
              icon: 'none',
              duration: 3000
            })
            that.setData({
              s_number: res.data.data,
            })
            wx.switchTab({
              url: '/pages/search-list/search-list',
            });
          } else {
            wx.showToast({
              title: res.data.MESSAGE,
              icon: 'none',
              duration: 3000
            })
          }
        }
      })
    }
  },

  goReset:function(){
    this.setData({
      s_name1: '',
      s_name2: '',
      selectTopName:'局部阻力系数查询',
      selectTopNamet:'局部阻力系数查询',
      selectTopName1:'',
      selectTopName2:'',
      fangyuan:'请选择',
      c_name: '',
      c_tname: '',
      c_horizontal: '',
      c_vertical: '',
      num1: '',
      num2: '',
      num3: '',
      num4: '',
      num5: '',
      num6: '',
      num7: '',
      num8: '',
      r_num1: '',
      r_num2: '',
      r_num3: '',
      r_num4: '',
      c_type:0
    });
    this.getTypeImg();
    wx.showToast({
      title: "重置成功",
      icon: 'none',
      duration: 3000
    })
  },

  
  goSmokeVentilation:function(){
    var that = this;
    wx.showLoading({
      title: '加载中',
    })
    // 正则表达式，匹配整数或小数
    //const reg = /^[0-9]*\.?[0-9]*$/;
    const reg = /^(?:\d+|\d+\.\d+)$/;

    // 如果输入值符合正则表达式，则更新数据
    if (!reg.test(that.data.num5)) {
        wx.showToast({
          title: "请检查输入有效的整数或者小数",
          icon: 'none'
        });
        return;
    }
    if (that.data.fangyuan == '请选择') {    
      wx.showToast({
        title: '请选择方管/圆管',
        icon: 'none'
      });
      return;
    }
    if (that.data.num2==0) {    
      wx.showToast({
        title: '阻力系数为0时，不能计算',
        icon: 'none'
      });
      return;
    }
    if (that.data.fangyuan == '圆管') { 
      if(!reg.test(that.data.num8)){
        wx.showToast({
          title: '圆管时需要填写风管直径',
          icon: 'none'
        });
        return;     
      }
    }else{
      if(!reg.test(that.data.num6)
        || !reg.test(that.data.num7)){
        wx.showToast({
          title: '方管时需要填写烟道尺寸',
          icon: 'none'
        });
        return;     
      }     
    }
    wx.request({
      url: app.domain_path + app.api_path + '/smoke_ventilation',
      method: 'post',
      data: {
        token: main.get_storage('token'),
        smoke_type:41,
        num1: that.data.num1,
        num2: that.data.num2,
        num3: that.data.num3,
        num4: that.data.num4,
        num5: that.data.num5,
        num6: that.data.num6,
        num7: that.data.num7,
        num8: that.data.num8,
        fangyuan:that.data.fangyuan
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function(res) {
        //console.log(res);return;
        if (!res.data) {
          wx.showToast({
            title: '加载错误',
            icon: 'loading',
            duration: 3000
          })
        }
        if (res.data.RESULT == 'OK') {
          wx.hideLoading();
          wx.showToast({
            title: '计算成功',
            icon: 'none',
            duration: 3000
          })
          that.setData({
            r_num1: res.data.data.smoke_ventilation_info.r_num1,
            r_num2: res.data.data.smoke_ventilation_info.r_num2,
            r_num3: res.data.data.smoke_ventilation_info.r_num3,
            r_num4: res.data.data.smoke_ventilation_info.r_num4
          })
        } else {
          wx.showToast({
            title: res.data.MESSAGE,
            icon: 'none',
            duration: 3000
          })
        }
      }
    })
  },

  getTypeImg:function(){
    var that = this;
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.domain_path + app.api_path + '/get_drag_coefficient',
      method: 'post',
      data: {
        token: main.get_storage('token'),
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function(res) {
        if (!res.data) {
          wx.showToast({
            title: '加载错误',
            icon: 'loading',
            duration: 3000
          })
        }
        if (res.data.RESULT == 'OK') {
          wx.hideLoading();
          that.setData({
            actions: res.data.data.drag_coefficient_arr1,
            selectTopName: res.data.c_tname,
            selectTopNamet: res.data.c_name,
            selectTopName1: res.data.c_vertical_c_1,
            selectTopName2: res.data.c_horizontal_c_2,
            c_name: res.data.c_name,
            c_tname: res.data.c_tname,
            //num2: res.data.c_values,
            c_img: res.data.c_img
          })
        } else {
          wx.showToast({
            title: res.data.MESSAGE,
            icon: 'none',
            duration: 3000
          })
        }
      }
    })
  },

  getnews:function(){
    var that = this;
    wx.request({
      url: app.domain_path + app.api_path + '/get_column_info',
      method: 'post',
      data: {
        token: main.get_storage('token'),
        id:41,
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function(res) {
        if (!res.data) {
          wx.showToast({
            title: '加载错误',
            icon: 'loading',
            duration: 3000
          })
        }
        if (res.data.RESULT == 'OK') {
          wx.hideLoading();
          that.setData({
            news: res.data.data
          })
          //商品描述富文本编辑器
          WxParse.wxParse('article', 'html', that.data.news, that, 5);
        } else {
          wx.showToast({
            title: res.data.MESSAGE,
            icon: 'none',
            duration: 3000
          })
        }
      }
    })
  },

  checkUserAuthorization: function () {
    const that = this;
    // 获取用户的授权信息
    wx.getSetting({
      success: (res) => {
        if (res.authSetting['scope.userInfo']) {
          console.log(main.get_storage('token') != '');
          if(main.get_storage('token') != []){
          }else{
            this.jumpConfirm();
          }
        } else {
          this.jumpConfirm();
        }
      },
      fail: (err) => {
        wx.showToast({
          title: err,
          icon: 'none'
        });
      }
    });
  },
  jumpConfirm:function(){
    wx.showModal({
      title: '温馨提示',
      content: '此功能需要授权登录，请确认~',
      success: (res) => {
        if (res.confirm) {
          wx.redirectTo({
            url: '/pages/index/index'
          });
        } else if (res.cancel) {
          wx.switchTab({
            url: '/pages/ser-list/ser-list',
          });
        }
      },
    });
  },
  onChange1: function(e) {
    this.setData({ show1: true });
  },
})